/**
 * 
 */
package com.base;

import java.util.List;

import com.base.enumeration.CheckRightConfEnum;
import com.base.enumeration.UserGroupEnum;


/**
 * @author 7738
 *
 */
public abstract class User {
	/**
	 * members
	 */
	private String id;
	private String code;
	private String name;
	private String password;
	private int language;
	
	/**
	 * getters and setters
	 * @return
	 */
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getLanguage() {
		return language;
	}

	public void setLanguage(int language) {
		this.language = language;
	}

	/**
	 * check wether user has right(s)
	 * @param operation
	 * @param operationObject
	 * @return
	 */
	protected abstract boolean checkRight(Operation operation, OperationObject operationObject);
	
	/**
	 * grant right(s) for user
	 * @param operation
	 * @param operationObject
	 * @return
	 */
	protected abstract List<Right> grantRight(Operation operation, OperationObject operationObject);
	
	/**
	 * judge right(s)
	 * @param operation
	 * @param operationObject
	 * @return
	 */
	public boolean hasRight(Operation operation, OperationObject operationObject) {
		// superuser
		if (UserGroupEnum.SUPERUSER.name().equals(this.getCode())) {
			return true;
		} else {
		// administrator or user
			System.out.println("=========");
			CheckRightConfEnum checkRightConfEnum = null;
			if (checkRightConfEnum == CheckRightConfEnum.IGNORE) {
				return true;
			} else {
				return this.checkRight(operation, operationObject);
			}
		}
		
	}
	
	/**
	 * common operation to perform crud action
	 * @param operation
	 * @param operationObject
	 * @return
	 */
	public Object doOperation(Operation operation, OperationObject operationObject) {
		System.out.println("exec doOperation.....");
		if (this.hasRight(operation, operationObject)) {
			List<Right> rights = this.grantRight(operation, operationObject);
			operation.setRights(rights);
			return operation.doOperation(operationObject);
		} else {
			return null;
		}
	}
	
}
